package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesEquipmentRunningRecordRpcService;
import com.hunttown.mes.rpc.domain.MesEquipmentRunningRecordDTO;
import com.hunttown.mes.rpc.domain.query.MesEquipmentRunningRecordDTOQuery;
import com.hunttown.mes.service.MesEquipmentRunningRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesEquipmentRunningRecordConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:26:50
 */
@Service
public class MesEquipmentRunningRecordRpcServiceImpl implements MesEquipmentRunningRecordRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesEquipmentRunningRecordRpcServiceImpl.class);

    private final MesEquipmentRunningRecordService mesEquipmentRunningRecordService;

    @Autowired
    public MesEquipmentRunningRecordRpcServiceImpl(MesEquipmentRunningRecordService mesEquipmentRunningRecordService) {
        this.mesEquipmentRunningRecordService = mesEquipmentRunningRecordService;
    }

    @Override
    public MesEquipmentRunningRecordDTO insert(MesEquipmentRunningRecordDTO mesEquipmentRunningRecordDTO) {
        try {
            return convert(mesEquipmentRunningRecordService.insert(convert(mesEquipmentRunningRecordDTO)));
        } catch (Exception e) {
            logger.error("MesEquipmentRunningRecord create failed.", e);
            throw new RuntimeException("MesEquipmentRunningRecord create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesEquipmentRunningRecordDTO mesEquipmentRunningRecordDTO) {
        try {
            return mesEquipmentRunningRecordService.updateInfoById(convert(mesEquipmentRunningRecordDTO));
        } catch (Exception e) {
            logger.error("MesEquipmentRunningRecord update by id failed.", e);
            throw new RuntimeException("MesEquipmentRunningRecord update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesEquipmentRunningRecordDTOQuery mesEquipmentRunningRecordDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentRunningRecordDTOQuery.getW_id() != null && mesEquipmentRunningRecordDTOQuery.getW_id() > 0) {
                query.put("w_id", mesEquipmentRunningRecordDTOQuery.getW_id());
            }

            if (mesEquipmentRunningRecordDTOQuery.getIds() != null && !mesEquipmentRunningRecordDTOQuery.getIds().equals("")) {
                query.put("ids", mesEquipmentRunningRecordDTOQuery.getIds());
            }

            if (mesEquipmentRunningRecordDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentRunningRecordDTOQuery.getDeleteFlag());
            }

            return mesEquipmentRunningRecordService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesEquipmentRunningRecord update by query failed.", e);
            throw new RuntimeException("MesEquipmentRunningRecord update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentRunningRecordDTO selectById(Integer id) {
        try {
            return convert(mesEquipmentRunningRecordService.selectById(id));
        } catch (Exception e) {
            logger.error("MesEquipmentRunningRecord select by id failed.", e);
            throw new RuntimeException("MesEquipmentRunningRecord select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentRunningRecordDTO selectObjByQuery(MesEquipmentRunningRecordDTOQuery mesEquipmentRunningRecordDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentRunningRecordDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentRunningRecordDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesEquipmentRunningRecordDTOQuery.getOrderBy())) {
                query.put("orderBy", mesEquipmentRunningRecordDTOQuery.getOrderBy());
            }

            return convert(mesEquipmentRunningRecordService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesEquipmentRunningRecord select obj by query failed.", e);
            throw new RuntimeException("MesEquipmentRunningRecord select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesEquipmentRunningRecordDTO> getListForPage(MesEquipmentRunningRecordDTOQuery mesEquipmentRunningRecordDTOQuery) {
        Query query = Query.page(mesEquipmentRunningRecordDTOQuery.getPn(), mesEquipmentRunningRecordDTOQuery.getPageSize());

        if (mesEquipmentRunningRecordDTOQuery.getId() != null && mesEquipmentRunningRecordDTOQuery.getId() > 0) {
            query.put("id", mesEquipmentRunningRecordDTOQuery.getId());
        }

        if (mesEquipmentRunningRecordDTOQuery.getIds() != null && !mesEquipmentRunningRecordDTOQuery.getIds().equals("")) {
            query.put("ids", mesEquipmentRunningRecordDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesEquipmentRunningRecordDTOQuery.getLikeName())) {
            query.put("likeName", mesEquipmentRunningRecordDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesEquipmentRunningRecordDTOQuery.getShowDate())) {
            query.put("showDate", mesEquipmentRunningRecordDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentRunningRecordDTOQuery.getStartDate())) {
            query.put("startDate", mesEquipmentRunningRecordDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentRunningRecordDTOQuery.getEndDate())) {
            query.put("endDate", mesEquipmentRunningRecordDTOQuery.getEndDate());
        }

        if (mesEquipmentRunningRecordDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesEquipmentRunningRecordDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesEquipmentRunningRecordDTOQuery.getOrderBy())) {
            query.put("orderBy", mesEquipmentRunningRecordDTOQuery.getOrderBy());
        }

        List<MesEquipmentRunningRecordDTO> list = convert(mesEquipmentRunningRecordService.selectByQuery(query));
        Page<MesEquipmentRunningRecordDTO> page = Page.from(mesEquipmentRunningRecordDTOQuery.getPn(), mesEquipmentRunningRecordDTOQuery.getPageSize(), list);
        page.setTotalItems(mesEquipmentRunningRecordService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesEquipmentRunningRecordService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesEquipmentRunningRecord delete failed.", e);
            throw new RuntimeException("MesEquipmentRunningRecord delete failed, Please order your code.", e);
        }
    }
}